import React from 'react';
import { connect } from 'dva';
import { Button, Table, Form, Input, Popconfirm,DatePicker, Select,TreeSelect,   } from 'antd';
const FormItem = Form.Item;
const Option = Select.Option;
const TreeNode = TreeSelect.TreeNode;
const RangePicker = DatePicker.RangePicker;
import CabinetGroupModal from '../../components/cabinet/CabinetGroupModal';


function CabinetGroup({ dispatch, cabinetGroup, loading, form }) {

    const { getFieldDecorator, getFieldsValue, getFieldValue } = form;
    const { currentItem, modalVisible, modalType} = cabinetGroup

    const columns = [{
        title: '机柜组名称',
        dataIndex: 'name',
        key: 'name',
        width: 120,
    },{
        title: '备注',
        dataIndex: 'remark',
        key: 'remark',
        width: 100,
    },{
        title: '创建时间',
        dataIndex: 'createTime',
        key: 'createTime',
        width: 100,
    },{
        title: '创建人',
        dataIndex: 'createName',
        key: 'createName',
        width: 80,
    },{
        title: '操作',
        key: 'operation',
        width: 60,
        render: (text, record) => (
            <span>
                <a onClick={editHandler.bind(null, record.id_)}>编辑</a>
                <span className="ant-divider" />
                <Popconfirm title="确认删除？" onConfirm={deleteHandler.bind(null, record.id_)}>
                <a>删除</a>
                </Popconfirm>
            </span>
        ),
    }];

    const pagination = {
        current:cabinetGroup.current,
        pageSize:cabinetGroup.currentSize,
        total: cabinetGroup.total,
        showSizeChanger: true,
        showTotal: (total => '共' + cabinetGroup.total + '条'),
        onShowSizeChange(current, size) {
            dispatch({
                type: 'cabinetGroup/queryList',
                payload: { ...getFieldsValue(),...{'pageNum':current, 'pageSize':size} },
            });
        },
        onChange(current) {
            dispatch({
                type: 'cabinetGroup/queryList',
                payload: { ...getFieldsValue(),...{'pageNum':current, 'pageSize':cabinetGroup.currentSize} },
            });
        },
    };

    //删除事件
    function deleteHandler(id) {
        dispatch({
            type: 'cabinetGroup/remove',
            payload: {id:id,search:{...getFieldsValue(),...{'pageNum':cabinetGroup.current, 'pageSize':cabinetGroup.currentSize}},},
        });
    }

    //查看对象
    const editHandler = (id, values) => {
        dispatch({
            type: 'cabinetGroup/qryById',
            payload:id,
        });
    }
    //查询事件
    const submitHandle = () => {
        dispatch({
            type: 'cabinetGroup/queryList',
            payload: { ...getFieldsValue() },
        });
    }
    //清空查询条件
    function handleClear(){
      form.resetFields();
    }

   //点击新增按钮
    const addHandler = () => {
        dispatch({
            type: 'cabinetGroup/showModal',
            payload: {
                modalType: 'create',
            },
        })
    }

    const modalProps = {
        item: cabinetGroup.currentItem,
        visible: modalVisible,
        type: modalType,
        maskClosable: false,
        dispatch: dispatch,
        confirmLoading: loading,
        title: `${modalType === 'create' ? '新增机柜组' : '编辑机柜组'}`,
        wrapClassName: 'vertical-center-modal',
        newKey : cabinetGroup.newKey,
        onOk (data) {
            dispatch({
                type: `cabinetGroup/${modalType}`,
                payload: {data:data,search:{...getFieldsValue(),...{'pageNum':cabinetGroup.current, 'pageSize':cabinetGroup.currentSize}}},
            })
        },
        onCancel () {
            dispatch({
                type: 'cabinetGroup/hideModal',
            })
        },
    }
    return (
        <div style={{backgroundColor:'#fff'}}>
          <div style={{minHeight:'100%'}}>
              <div style={{width:'100%',backgroundColor:'#fff',lineHeight:'40px',paddingLeft:'20px',paddingTop:'10px'}}>
                  <Form layout="inline" >
                    <FormItem>
                       {getFieldDecorator('name')(
                          <Input type="text" placeholder="机柜组名称" style={{ width:'200px' }} />
                        )}
                    </FormItem>
                  
                    <FormItem>
                        <Button type="primary" size='large' onClick={submitHandle} className='ant-search-btn'>查询</Button>
                    </FormItem>
                    <FormItem>
                        <Button size="large" htmlType="reset" onClick={handleClear}>清空</Button>
                    </FormItem>

                    <div style={{marginTop:'-40px',textAlign:'right',paddingRight:'10px'}}>
                        <FormItem>
                            <Button type="primary" size='large' onClick={addHandler}>新增机柜组</Button>
                        </FormItem>
                    </div>

                </Form>
            </div>
            <div style={{width:'100%',padding:"30px 20px",}}>
                <Table columns={columns} rowKey={record => record.id_} loading={loading} dataSource={cabinetGroup.list} pagination={pagination}
                />
            </div>
            </div>
            <CabinetGroupModal {...modalProps} />

        </div>
    );
}

function mapStateToProps(state) {
  return { cabinetGroup : state.cabinetGroup,loading:state.loading.models.cabinetGroup};
}

CabinetGroup = Form.create()(CabinetGroup);

export default connect(mapStateToProps)(CabinetGroup);
